package com.sync.syncmasterdata.configuration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.util.Map;

/**
 * 美博特的数据库链接
 */
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef="entityManagerFactorySix",
        transactionManagerRef="transactionManagerSix",
        basePackages= {"com.sync.syncmasterdata.outsystem.repositorymbt"})//设置dao（repo）所在位置
public class SixConfig {
    @Autowired
    @Qualifier("dataSourceSix")
    private DataSource secondDataSource;

    @Autowired
    @Qualifier("vendorProperties")
    private Map<String, Object> vendorProperties;


    @Bean(name = "entityManagerFactorySix")
    public LocalContainerEntityManagerFactoryBean entityManagerFactorySix(EntityManagerFactoryBuilder builder) {
        LocalContainerEntityManagerFactoryBean entityManagerFactory = builder
                .dataSource(secondDataSource)
                .properties(vendorProperties)
                .packages("com.sync.syncmasterdata.outsystem.entitymbt")
                .persistenceUnit("sixPersistenceUnit")
                .build();
        return entityManagerFactory;
    }

    @Bean(name = "entityManagerSix")
    public EntityManager entityManagerSix(EntityManagerFactoryBuilder builder) {
        return entityManagerFactorySix(builder).getObject().createEntityManager();
    }


    @Bean(name = "transactionManagerSix")
    PlatformTransactionManager transactionManagerSix(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactorySix(builder).getObject());
    }

}
